/**
 * Author : wang885298.
 * Date   : 2018/6/15.
 * File   : regist-components.注册自定义组件.
 */
import msg from './components/msg.vue'
/**
 * 全局注册各组件
 * @param Vue
 */
let num = 10
function registComponents (Vue) {
  Vue.component('msg', msg)
  Vue.directive('dragable', {
    inserted (el, binding) {
      if (binding.value) {
        return
      }
      el.style.cursor = 'move'
      el.onmousedown = (e) => {
        if (e.target.childNodes.length < 1) {
          return
        }
        el.style.zIndex = num++
        var oevent = e || window.event
        var distanceX = oevent.clientX - el.offsetLeft
        var distanceY = oevent.clientY - el.offsetTop
        var startX = oevent.clientX
        var startY = oevent.clientY
        document.onmouseup = (e) => {
          document.onmousemove = null
          document.onmousemove = null
        }
        document.onmousemove = (e) => {
          var oevent = e || window.event
          oevent.preventDefault()
          var movingLessX = Math.abs(startX - oevent.clientX)
          var movingLessY = Math.abs(startY - oevent.clientY)
          if (movingLessX > 4) {
            el.style.left = oevent.clientX - distanceX + 'px'
          }
          if (movingLessY > 4) {
            el.style.top = oevent.clientY - distanceY + 'px'
          }
        }
      }
    }
  })
}

export default registComponents
